$(document).ready(function(){
	var id = $.trim($("#id").val());
	if(id != ""){
		$("#passDiv").hide();
	}else{
		$("#passDiv").show();
	}
	$('#role_id').combobox({
		url : appCtx + "system/role/loadRolesSelect.html",
		valueField : 'id',
		textField : 'name',
		editable : true,
		onSelect : function(data) {
			$('#role_id').val(data.id);
		},onLoadSuccess : function() {
			if(id == ""){
				$('#role_id').combobox('setValue', "请选择所属角色");
			}
		}
	});
	var url = "";
	if(userType==3){
		url=appCtx + "base/notifyRecore/queryAllDriver.html";
	}else{
		url=appCtx + "base/notifyRecore/queryAllEnterprise.html";
	}
	$("#link_id").select2({
		theme: "default",
        allowClear: true,
        placeholder: "请输入用户类型名称查询",
        ajax:{
        	url : url,
            dataType:"json",
            delay:300,
            data:function(params){
            	if(params.term == undefined) {
            		params.term = "";
            	}
                return {
                	name:params.term
                };
            },
            cache:true,
            processResults: function (res) {
		    	if(res){
		    		var options = [];
    				$.each(res,function(index,obj) {
						var option = {"id":obj.id, "text":obj.name};
	                    options.push(option);
    				});
	                return {
	                	results: options,
	                    pagination: {
	                        more:false
	                    }
	                };
		    	}
            },
            escapeMarkup: function (markup) { return markup; },
            minimumInputLength: 1
        }
    });
	if (link_name!=null&&link_name!='') {
		$("#link_id").html("<option value='" + link_id + "' selected>" + link_name + "</option>").trigger("change");
	}
	
	$('#area_id').combotree({
		url:appCtx + "/system/common/queryAreaTree.html",
		valueField: 'id',
        textField: 'text',
        method: 'post',
        editable : false,
        collapseAll : true,
        onSuccess : function(data){
        	$('#area_id').val(data.id);
        },            
        loadFilter: function(data){
        	 data.unshift({ id: '', text: '请选择车辆管辖区域', selected: "selected" });
             return data;
		},
		onLoadSuccess: function (data) {
			if(id == ""){
				$('#area_id').combotree('tree').tree("collapseAll");
				$('#area_id').combotree('setValue','')
			}
     	}
	});
	initData();
});
function initData(){
	var me = this;
	var url = "";
	$('#user_type').on('change',function(){
		var user_type=$("#user_type").val();
		if(user_type==2){
			$("#link_id").empty();
			url=appCtx + "base/notifyRecore/queryAllEnterprise.html";
			me.bind(url);
		}else if(user_type==3){
			$("#link_id").empty();
			url=appCtx + "base/notifyRecore/queryAllDriver.html";
			me.bind(url);
		}
	});
}
/**
 * 保存用户
 * */
function saveUser() {
	var id = $.trim($("#id").val());
	var url = "";
	if(id != ""){
		url = appCtx + "system/user/updateUser.html";
	}else{
		url = appCtx + "system/user/saveUser.html";
	}
	
	var flag = true;
	
	var login_name = $("#login_name").val();
	if(login_name == ""){
		flag = false ;
		showVelidateModal('登录名不能为空!');
		return;
	}
	if(login_name.indexOf(" ") >= 0){
		flag = false ;
		showVelidateModal('登录名不能含有空格!');
		return;
	}
	var user_name = $.trim($("#user_name").val());
	if(user_name == ""){
		flag = false ;
		showVelidateModal('用户名不能为空!');
		return;
	}
	
	var area_id = $("#area_id").combotree("getValue");
	if(area_id == ""){
		flag = false ;
		showVelidateModal('车辆管辖区域不能为空!');
		return;
	}
	
	var role_id = $("#role_id").combotree("getValue");
	
	if(role_id == "请选择所属角色" ||role_id == ''|| typeof(role_id) == 'undefined' ){
		flag = false ;
		showVelidateModal('所属角色不能为空!');
		return;
	}
	
	var link_id = $.trim($("#link_id").val());
	if(link_id == ""){
		flag = false ;
		showVelidateModal('用户类型名称不能为空!');
		return;
	}
	
	
	if (flag) {
		$.ajax({
			type : 'post',
			async : false,
			url : url,
			data : $("#addUserForm").serialize(),
			cache : false,
			dataType : "JSON",
			success : function(data) {
				if (data.code == "0") {
					global.ajaxLoading.pop('操作成功!',false,2000);
					closeDataDialog();
					var funStr = "window.frames['" + mFrameName + "']."+ mDialogCallback;
					eval("(" + funStr + ")");
				}else if (data.code == "1") {
					showVelidateModal("操作失败,原因是已经存在相同用户名!");
					return;
				}  else {
					showVelidateModal("操作失败!");
					return;
				}
			},
			error : function(hxr, s, e) {
			}
		});
	}
};
function bind(url){
	$("#link_id").select2({
		theme: "default",
        allowClear: true,
        placeholder: "请输入用户名称查询",
        ajax:{
        	url : url,
            dataType:"json",
            delay:300,
            data:function(params){
            	if(params.term == undefined) {
            		params.term = "";
            	}
                return {
                	name:params.term
                };
            },
            cache:true,
            processResults: function (res) {
		    	if(res){
		    		var options = [];
    				$.each(res,function(index,obj) {
						var option = {"id":obj.id, "text":obj.name};
	                    options.push(option);
    				});
	                return {
	                	results: options,
	                    pagination: {
	                        more:false
	                    }
	                };
		    	}
            },
            escapeMarkup: function (markup) { return markup; },
            minimumInputLength: 1
        }
    });
};
